"""
给定一个二叉树，找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明：叶子节点是指没有子节点的节点。
示例 1：
输入：root = [3,9,20,null,null,15,7]
输出：2
示例 2：
输入：root = [2,null,3,null,4,null,5,null,6]
输出：5

链接：https://leetcode-cn.com/problems/minimum-depth-of-binary-tree
"""
from mode import *


class Solution:
    def minDepth(self, root: TreeNode) -> int:
        if root is None:
            return 0

        deque = collections.deque()
        deque.append(root)
        depth = 1

        while deque is not None:
            size = len(deque)
            for i in range(size):
                cur = deque.popleft()
                if cur.left is None and cur.right is None:
                    return depth
                if cur.left:
                    deque.append(cur.left)
                if cur.right:
                    deque.append(cur.right)
            depth += 1

        return depth


class Solution1:
    def minDepth(self, root: TreeNode) -> int:
        if not root:
            return 0
        deque = collections.deque([root])
        depth = 1
        while deque:
            size = len(deque)
            for i in range(size):
                node = deque.popleft()
                if not node.left and not node.right:
                    return depth

                if node.left:
                    deque.append(node.left)
                if node.right:
                    deque.append(node.right)
            depth += 1

        return depth


if __name__ == "__main__":
    """
                3
         9             20
                 15           7 
    """
    tree = '[3,9,20,null,null,15,7]'
    n = mode.generateTreeNode(tree)
    A = Solution()
    print(A.minDepth(n))
    A = Solution1()
    print(A.minDepth(n))
